package com.xingdata.jjxc.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xingdata.jjxc.R;
import com.xingdata.jjxc.R.drawable;
import com.xingdata.jjxc.R.id;
import com.xingdata.jjxc.R.layout;
import com.xingdata.jjxc.base.BaseActivity;
import com.xingdata.jjxc.enty.TravelEntity;

/**
 * 
 * @Description: TODO(总行程)
 * @date 2015-10-14 下午4:31:43 Creater JiangMin
 */
public class HabitesActivity extends BaseActivity implements OnClickListener {
	private TextView title;
	private ArrayList<TravelEntity> travelEntities = new ArrayList<TravelEntity>();
	private ListView travelListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_habites;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "总行程";
	}

	@Override
	protected void initViews() {
		testDB();
		travelListView = (ListView) findViewById(R.id.totaltravel_listview);
		travelListView.setAdapter(new mAdapter());

	}

	private void testDB() {
		TravelEntity entity = new TravelEntity();
		entity.setTitle("总点火次数");
		entity.setInfo("20" + "次");
		entity.setLengh("300");
		entity.setClolour(R.drawable.shape_line_blue);
		entity = new TravelEntity();
		travelEntities.add(entity);
		entity.setTitle("累计行驶时间");
		entity.setInfo("100" + "h");
		entity.setLengh("150");
		entity.setClolour(R.drawable.shape_line_orange);
		travelEntities.add(entity);
		entity = new TravelEntity();
		entity.setTitle("累计怠速时间");
		entity.setInfo("5" + "h");
		entity.setLengh("250");
		entity.setClolour(R.drawable.shape_line_red);
		travelEntities.add(entity);
		entity = new TravelEntity();
		entity.setTitle("平均热车时间");
		entity.setInfo("50" + "s");
		entity.setLengh("650");
		entity.setClolour(R.drawable.shape_line_grey);
		travelEntities.add(entity);
		entity = new TravelEntity();
		entity.setTitle("平均车速");
		entity.setInfo("10" + "km/h");
		entity.setLengh("250");
		entity.setClolour(R.drawable.shape_line_blue);
		travelEntities.add(entity);
		entity = new TravelEntity();
		entity.setTitle("历史最高车速");
		entity.setInfo("180" + "km/h");
		entity.setLengh("650");
		entity.setClolour(R.drawable.shape_line_blue);
		travelEntities.add(entity);
		entity = new TravelEntity();
		entity.setTitle("历史最高转速");
		entity.setInfo("10" + "rpm");
		entity.setLengh("150");
		entity.setClolour(R.drawable.shape_line_orange);
		travelEntities.add(entity);
		entity = new TravelEntity();
		entity.setTitle("累计急加速时间");
		entity.setInfo("5" + "Times");
		entity.setLengh("224");
		entity.setClolour(R.drawable.shape_line_green);
		travelEntities.add(entity);
		entity = new TravelEntity();
		entity.setTitle("累计急减速时间");
		entity.setInfo("5" + "Times");
		entity.setLengh("60");
		entity.setClolour(R.drawable.shape_line_orange);
		travelEntities.add(entity);
		entity = new TravelEntity();
		entity.setTitle("2" + "Times");
	}

	public class mAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return travelEntities.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			LayoutInflater mInflater = LayoutInflater
					.from(HabitesActivity.this);
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(
						R.layout.carinfo_totaltravel_item, null);
				holder.line = (ImageView) convertView
						.findViewById(R.id.totaltravel_line);
				holder.title = (TextView) convertView
						.findViewById(R.id.totaltravel_title);
				holder.info = (TextView) convertView
						.findViewById(R.id.totaltravel_info);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.info.setText(travelEntities.get(position).getInfo());
			LayoutParams params = holder.line.getLayoutParams();
			params.width = Integer.parseInt(travelEntities.get(position)
					.getLengh());
			params.height = 14;
			holder.line.setLayoutParams(params);
			holder.line.setBackgroundResource(travelEntities.get(position)
					.getClolour());
			holder.title.setText(travelEntities.get(position).getTitle());
			return convertView;
		}
	}

	private final class ViewHolder {
		private ImageView line;
		private TextView info;
		private TextView title;
	}

}
